package com.stylefeng.guns.rest.modular.film;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.rpc.RpcContext;
import com.stylefeng.guns.api.film.FilmAsyncServiceAPI;
import com.stylefeng.guns.api.film.FilmServiceAPI;
import com.stylefeng.guns.api.film.vo.*;
import com.stylefeng.guns.rest.modular.film.vo.FilmConditionVO;
import com.stylefeng.guns.rest.modular.film.vo.FilmIndexVO;
import com.stylefeng.guns.rest.modular.film.vo.FilmRequestVO;
import com.stylefeng.guns.rest.modular.vo.ReponseVo;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@RequestMapping("/film/")
@RestController
public class FilmController {

    private static final String IMG_PRE = "http://img.meetngshop.cn/";

    @Reference(interfaceClass = FilmServiceAPI.class, check = false)
    private FilmServiceAPI filmServiceAPI;

    @Reference(interfaceClass = FilmAsyncServiceAPI.class, check = false, async = true)
    private FilmAsyncServiceAPI filmAsyncServiceAPI;

    @RequestMapping(value = "getIndex", method = RequestMethod.GET)
    public ReponseVo getIndex() {
        FilmIndexVO filmIndexVO = new FilmIndexVO();
        //获取banner信息
        filmIndexVO.setBanners(filmServiceAPI.getBanners());
        //获取热映信息
        filmIndexVO.setHotFilms(filmServiceAPI.getHotFilms(true, 8, 1, 1, 99, 99, 99));
        //获取即将上映
        filmIndexVO.setSoonFilms(filmServiceAPI.getSoonFilms(true, 8, 1, 1, 99, 99, 99));
        //获取票房排行
        filmIndexVO.setBoxRanking(filmServiceAPI.getBoxRanking());
        //获取人气榜
        filmIndexVO.setExpectRanking(filmServiceAPI.getExpectRanking());
        //top100
        filmIndexVO.setTop100(filmServiceAPI.getTop());
        return ReponseVo.success(IMG_PRE, filmIndexVO);
    }

    @RequestMapping(value = "getConditionList", method = RequestMethod.GET)
    public ReponseVo getConditionList(@RequestParam(value = "catId", required = false, defaultValue = "99") String catId,
                                      @RequestParam(value = "sourceId", required = false, defaultValue = "99") String sourceId,
                                      @RequestParam(value = "yearId", required = false, defaultValue = "99") String yearId) {
        //标识
        boolean flag = false;
        //类型集合
        List<CatVO> cats = filmServiceAPI.getCats();
        List<CatVO> catResult = new ArrayList<>();
        CatVO cat = new CatVO();
        for (CatVO catVO : cats) {
            //判断集合是否存在catid 如果存在 则将对应的实体变成active
            if (catVO.getCatId().equals("99")) {
                cat = catVO;
                continue;
            }
            if (catVO.getCatId().equals(catId)) {
                flag = true;
                catVO.setActive(true);
            } else {
                catVO.setActive(false);
            }
            catResult.add(catVO);
        }
        //如果不存在，在默认全部变成avcive状态
        if (!flag) {
            cat.setActive(true);
            catResult.add(cat);
        } else {
            catResult.add(cat);
        }

        //片源集合
        flag = false;
        List<SourceVO> sources = filmServiceAPI.getSources();
        List<SourceVO> sourceResult = new ArrayList<>();
        SourceVO source = new SourceVO();
        for (SourceVO sourceVO : sources) {
            //判断集合是否存在catid 如果存在 则将对应的实体变成active
            if (sourceVO.getSourceId().equals("99")) {
                source = sourceVO;
                continue;
            }
            if (sourceVO.getSourceId().equals(sourceId)) {
                flag = true;
                sourceVO.setActive(true);
            } else {
                sourceVO.setActive(false);
            }
            sourceResult.add(sourceVO);
        }
        //如果不存在，在默认全部变成avcive状态
        if (!flag) {
            source.setActive(true);
            sourceResult.add(source);
        } else {
            source.setActive(false);
            sourceResult.add(source);
        }

        //年代集合
        flag = false;
        List<YearVO> years = filmServiceAPI.getYears();
        List<YearVO> yearResult = new ArrayList<>();
        YearVO year = new YearVO();
        for (YearVO yearVO : years) {
            //判断集合是否存在catid 如果存在 则将对应的实体变成active
            if (yearVO.getYearId().equals("99")) {
                year = yearVO;
                continue;
            }
            if (yearVO.getYearId().equals(yearId)) {
                flag = true;
                yearVO.setActive(true);
            } else {
                yearVO.setActive(false);
            }
            yearResult.add(yearVO);
        }
        //如果不存在，在默认全部变成avcive状态
        if (!flag) {
            year.setActive(true);
            yearResult.add(year);
        } else {
            year.setActive(false);
            yearResult.add(year);
        }
        FilmConditionVO filmConditionVO = new FilmConditionVO();
        filmConditionVO.setCatInfo(catResult);
        filmConditionVO.setSourceInfo(sourceResult);
        filmConditionVO.setYearInfo(yearResult);
        return ReponseVo.success(filmConditionVO);
    }

    @RequestMapping(value = "getFilms", method = RequestMethod.GET)
    public ReponseVo getFilms(FilmRequestVO filmRequestVO) {

        FilmVO filmVO = null;
        //根据showtype判断查询的影片类型
        switch (filmRequestVO.getShowType()) {
            case 1:
                filmVO = filmServiceAPI.getHotFilms(false,
                        filmRequestVO.getPageSize(), filmRequestVO.getNowPage(),
                        filmRequestVO.getSortId(), filmRequestVO.getSourceId(),
                        filmRequestVO.getYearId(), filmRequestVO.getCatId());
                break;
            case 2:
                filmVO = filmServiceAPI.getSoonFilms(false,
                        filmRequestVO.getPageSize(), filmRequestVO.getNowPage(),
                        filmRequestVO.getSortId(), filmRequestVO.getSourceId(),
                        filmRequestVO.getYearId(), filmRequestVO.getCatId());
                break;
            case 3:
                filmVO = filmServiceAPI.getClassicFilms(
                        filmRequestVO.getPageSize(), filmRequestVO.getNowPage(),
                        filmRequestVO.getSortId(), filmRequestVO.getSourceId(),
                        filmRequestVO.getYearId(), filmRequestVO.getCatId());
                break;
            default:
                filmVO = filmServiceAPI.getHotFilms(false,
                        filmRequestVO.getPageSize(), filmRequestVO.getNowPage(),
                        filmRequestVO.getSortId(), filmRequestVO.getSourceId(),
                        filmRequestVO.getYearId(), filmRequestVO.getCatId());
                break;
        }
        //根据sortid排序
        //添加各种条件
        //判断当前是第几页
        return ReponseVo.success(filmVO.getNowPage(), filmVO.getTotalPage(), IMG_PRE, filmVO.getFilmInfo());
    }

    @RequestMapping(value = "films/{searchParam}", method = RequestMethod.GET)
    public ReponseVo films(@PathVariable("searchParam") String searchParam, int searchType) throws ExecutionException, InterruptedException {
        //根据searchType 判断查询类型

        //不通的查询类型 传入的查询条件不同

        //查询影片的详细信息

        FilmdetailVO filmDetail = filmServiceAPI.getFilmDetail(searchType, searchParam);

        if (filmDetail == null) {
            return ReponseVo.serviceFail("没有可查询的影片");
        } else if (filmDetail.getFilmId() == null || filmDetail.getFilmId().trim().length() == 0) {
            return ReponseVo.serviceFail("没有可查询的影片");
        }

        String filmId = filmDetail.getFilmId();

        //获取影片描述信息(同步)
        //FilmDescVO filmDescVO = filmServiceAPI.getFilmDesc(filmId);

        //获取影片描述信息(异步)
        filmAsyncServiceAPI.getFilmDesc(filmId);
        Future<FilmDescVO> filmDescVOFuture = RpcContext.getContext().getFuture();

        //获取图片信息(同步)
        //ImgVO imgVO = filmServiceAPI.getFilmImgs(filmId);

        //获取图片信息((异步)
        filmAsyncServiceAPI.getFilmImgs(filmId);
        Future<ImgVO> imgVOFuture = RpcContext.getContext().getFuture();

        //获取导演信息(同步)
        //ActorVO directorVO = filmServiceAPI.getDectInfo(filmId);

        //获取导演信息(异步)
        filmAsyncServiceAPI.getDectInfo(filmId);
        Future<ActorVO> actorVOFuture = RpcContext.getContext().getFuture();

        //获取演员信息(同步)
        //List<ActorVO> actors = filmServiceAPI.getActors(filmId);

        //获取演员信息(异步)
        filmAsyncServiceAPI.getActors(filmId);
        Future<List<ActorVO>> actorsFuture = RpcContext.getContext().getFuture();

        ActorRequestVO actorRequestVO = new ActorRequestVO();
        actorRequestVO.setActors(actorsFuture.get());
        actorRequestVO.setDirector(actorVOFuture.get());

        InfoRequestVO infoRequestVO = new InfoRequestVO();
        infoRequestVO.setActors(actorRequestVO);
        infoRequestVO.setBiography(filmDescVOFuture.get().getBiography());
        infoRequestVO.setFilmId(filmId);
        infoRequestVO.setImgs(imgVOFuture.get());

        filmDetail.setInfo04(infoRequestVO);
        return ReponseVo.success(IMG_PRE, filmDetail);
    }

}
